<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦 2014年8月10日10:43:56
 * @Email:ikphp@sina.cn
 * @我关注的人 关注我的人 邀请好友 下期开发
 */
namespace IKPHP\Apps\Space\Controllers;

use IKPHP\Apps\Space\Controllers;
use IKPHP\Common\Models\User;
use IKPHP\Apps\Space\Models\UserFollow;

class ContactController extends SpaceBaseController {
	
	private $userid = 0;

    public function initialize()
    {
        parent::initialize();
    	// 访问者控制
		if ($this->visitor){
			$this->userid = $this->visitor['userid'];
		}
    }
    // 关注某人
	public function userfollowAction($fid){
		//访问控制
		if(!$this->visitor){return $this->_redirect('home/user/login');}
		
		$userid = $this->userid;
		if (!is_numeric($fid) || empty($fid)){
			return $this->error ( '呃...你想要的东西不在这儿' );
		}
		$userid_follow = $fid;//要关注人的id		
		//判断是否是自己
		if($userid == $fid){
			return $this->error ( '呃...自己不能关注自己' );
		}
		//判断是否已经关注过了
		$isFollow = UserFollow::isFollow($userid, $userid_follow);
		if($isFollow){
			return $this->error("请不要重复关注同一用户！");
		}else{
			$result = UserFollow::follow_user($userid, $userid_follow);
			if($result){
				//发送消息 
				//TODO:下次开发这个功能
				
				// 来路
				$strUser = User::getOneUser( $userid_follow );
				return $this->_redirect ( 'space/'.$strUser['doname']);
				
			}else{
				return $this->error("关注该用户失败；请联系网站管理员！");
			}
		}
	}
	//ajax 关注某人
	public function userfollowAjaxAction(){
		if(IS_AJAX){
			$ck = $this->_post('ck');
			$userid = $this->userid;
			$userid_follow = $this->_post('userid_follow'); 
			if($this->visitor['site_ck'] == $ck && !empty($ck)){
				
				//判断是否是自己
				if($userid == $userid_follow){
					return $this->ajaxReturn(array('res'=>0,'msg'=>'呃...自己不能关注自己'));
				}
								
				//判断是否已经关注过了
				$isFollow = UserFollow::isFollow($userid, $userid_follow);
				if($isFollow){
					return $this->ajaxReturn(array('res'=>0,'msg'=>'不要重复关注'));
				}

				$result = UserFollow::follow_user($userid, $userid_follow);
				if($result){
					$strUser = User::getOneUser($userid);
					$data['res'] = 1;
					$data['cout_follow'] = $strUser['count_follow'];
					return $this->ajaxReturn($data);				
				}
			}else{			
					$data['res'] = 2;
					return $this->ajaxReturn($data);
			}
			return false;
		}else{
			return $this->error ( '呃...你想要的东西不在这儿' );
		}
	}
	//关注我的人
	public function followedAction($userid){
		//访问控制
		if(!$this->visitor){return $this->_redirect('home/user/login');}
				
		if (!is_numeric($userid) || empty($userid)){
			return $this->error ( '呃...你想要的东西不在这儿' );
		}	
		$userid = (int)$userid;
		$strUser = User::getOneUser($userid);
		
		if(!empty($strUser['userid'])){
			//关注我的人
			$count = UserFollow::count("userid_follow ='$userid'");
			$pager = $this->_pager($count, 15);
			$where = array(
					"columns" => "userid",
			 		"conditions" => "userid_follow = '$userid' ",
		 			"order" => "addtime desc", 
		 			"limit" => array("number"=>$pager->listRows, 'offset'=>$pager->firstRow)
			);
			$list  = UserFollow::find($where);
			
			$arrFollowedUser = array();
			if($list){
				foreach( $list->toArray() as $key=>$item){
					$arrFollowedUser[$key] = User::getOneUser($item['userid']);
					$arrFollowedUser[$key]['isfollow'] = UserFollow::isFollow($this->userid, $item['userid']);
				}
			}
			
			$this->assign ( 'arrFollowedUser', $arrFollowedUser );
			$this->assign('pageUrl', $pager->show());
				
			if($userid == $this->userid)
			{
				$title = '关注我的人';
			}else{
				$title = "关注".$strUser['username']."的人(". $strUser['count_followed'] .")";
			}
			
			$this->assign ( 'strUser', $strUser );		
			$this->_config_seo ( array (
					'title' => $title,
					'subtitle' => '用户',
					'userTitle' => $title
			) );
			
		}else{
			
			return $this->error('您访问的页面不存在！');
			
		}
		
	}  
	// 我关注的人
	public function followAction($userid){
		//访问控制
		if(!$this->visitor){return $this->_redirect('home/user/login');}
				
		if (!is_numeric($userid) || empty($userid)){
			return $this->error ( '呃...你想要的东西不在这儿' );
		}	
		$userid = (int)$userid;
		$strUser = User::getOneUser($userid);	
		if(!empty($strUser['userid'])){

			$count = UserFollow::count("userid ='$userid'");
			$pager = $this->_pager($count, 15);
			$where = array(
					"columns" => "userid_follow",
			 		"conditions" => "userid = '$userid' ",
		 			"order" => "addtime desc", 
		 			"limit" => array("number"=>$pager->listRows, 'offset'=>$pager->firstRow)
			);
			$list  = UserFollow::find($where);
			
			$arrFollowUser = array();
			if($list){
				foreach( $list->toArray() as $key=>$item){
					$arrFollowUser[$key] = User::getOneUser($item['userid_follow']);
				}
			}
			
			$this->assign ( 'arrFollowUser', $arrFollowUser );
			$this->assign('pageUrl', $pager->show());
			
			if($userid == $this->userid)
			{
				$title = '我关注的人';
			}else{
				$title = $strUser['username']."关注的人(". $strUser['count_follow'] . ")";
			}	
				
			$this->assign ( 'strUser', $strUser );
			$this->_config_seo ( array (
					'title' => $title,
					'subtitle' => '用户',
					'userTitle' => $title
			) );
			
		}else{
			return $this->error('您访问的页面不存在！');
		}			
	} 
	//取消关注
	public function  unfollowAction($userid){
		//访问控制
		if(!$this->visitor){return $this->_redirect('home/user/login');}
				
		if (!is_numeric($userid) || empty($userid)){
			return $this->error ( '呃...你想要的东西不在这儿' );
		}	
		$userid = (int)$userid;//对方ID
		$myuserid = $this->userid;
		$strUser = User::getOneUser($userid);
		if(!empty($strUser['userid'])){
			//开始取消
			$result = UserFollow::unfollow_user($myuserid, $userid);
			if($result){
				return $this->_redirect('space/'.$strUser['doname']);
			}else{
				return $this->error('操作失败');
			}
		}else{
			return $this->error('您访问的页面不存在！');
		}
	}
	//ajax 取消关注
	public function unfollowAjaxAction(){
		if(IS_AJAX){
			$ck = $this->_post('ck');
			$userid = $this->userid;
			$userid_follow = $this->_post('userid');
			if($this->visitor['site_ck'] == $ck && !empty($ck)){
				$result = UserFollow::unfollow_user($userid, $userid_follow);
				if($result){
					$strUser = User::getOneUser($userid);
					$data['res'] = 1;
					$data['cout_follow'] = $strUser['count_follow'];
					return $this->ajaxReturn($data);				
				}else{
					return $this->ajaxReturn(array('res'=>0,'msg'=>'取消失败'));
				}
			}
			return $this->ajaxReturn(array('res'=>0,'msg'=>'没有登录是无法执行该操作'));
		}else{
			return $this->error ( '呃...你想要的东西不在这儿' );
		}		
	}
}